import Vue from "vue";
import Router from "vue-router";
import store from "../store";
import utils from "@/assets/js/utils";
import { post } from "@/assets/js/request";

Vue.use(Router);
let siteInfos = utils.getStorage("SITE_INFOS");
let routes = new Router({
  mode: "hash",
  routes: [
    {
      path: "/",
      component: () => import("@/views/Main.vue"),
      meta: {
        requireAuth: false,
        keepAlive: false,
      },
      children: [
        {
          path: "/",
          name: "home",
          component: () => import("@/views/Home.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/test",
          name: "test",
          component: () => import("@/views/test.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/led",
          name: "led",
          component: () => import("@/views/Led.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/notice",
          name: "notice",
          component: () => import("@/views/census/Notice.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/record",
          name: "record",
          component: () => import("@/views/census/Record.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/knowledge",
          name: "knowledge",
          component: () => import("@/views/Knowledge.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/monitor",
          name: "monitor",
          component: () => import("@/views/census/Monitor.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/monitorAll",
          name: "monitorAll",
          component: () => import("@/views/census/MonitorAll.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/playback",
          name: "playback",
          component: () => import("@/views/census/Playback.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/setting",
          name: "setting",
          component: () => import("@/views/census/Setting.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },

          children: [
            {
              path: "/setting",
              name: "settingAccount",
              component: () => import("@/views/census/SettingAccount.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/settingArea",
              name: "settingArea",
              component: () => import("@/views/census/SettingArea.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/settingHome",
              name: "settingHome",
              component: () => import("@/views/census/SettingHome.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/settingRole",
              name: "settingRole",
              component: () => import("@/views/census/SettingRole.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/settingHandle",
              name: "settingHandle",
              component: () => import("@/views/census/SettingHandle.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/settingIOT",
              name: "settingIOT",
              component: () => import("@/views/census/SettingIOT.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              //通知设置
              path: "/settingUpdateContent",
              name: "settingUpdateContent",
              component: () =>
                import("@/views/census/settingUpdateContent.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
          ],
        },
        {
          path: "/maintenance",
          name: "maintenance",
          component: () => {
            let siteInfos = utils.getStorage("SITE_INFOS");
            if (siteInfos.openMaintenance == 1) {
              window.location.href =
                "http://121.89.184.226/wb/index.html?refreshToken=" +
                siteInfos.refresh_token +
                "&flag=1&accountName=" +
                siteInfos.accountname;
              //window.location.href = 'http://localhost:63343/fireprotectionadmin/index.html?refreshToken=' + siteInfos.refresh_token + "&flag=1&accountName=" + siteInfos.accountname;
            } else {
              alert("请前往维保系统注册");
              //window.location.href = '/index.html'
            }
          },
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/equipment",
          name: "equipment",
          component: () => import("@/views/Equipment.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/emergency",
          name: "emergency",
          component: () => import("@/views/emergency/index.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/certificates",
          name: "certificates",
          component: () => import("@/views/certificates/index.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/historyAlarm",
          name: "historyAlarm",
          component: () => import("@/views/HistoryAlarm.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/bigDataAnalysis",
          name: "bigDataAnalysis",
          component: () => import("@/views/BigDataAnalysis.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/bigDataAnalysis",
          name: "bigDataAnalysis",
          component: () => import("@/views/BigDataAnalysis.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/archive",
          name: "archive",
          component: () => import("@/views/archive/Archive.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
          children: [
            {
              path: "/archive",
              name: "archiveOverview",
              component: () => import("@/views/archive/Overview.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/archiveRecord",
              name: "archiveRecord",
              component: () => import("@/views/archive/Record.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/archiveDocument",
              name: "archiveDocument",
              component: () => import("@/views/archive/Document.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/picture",
              name: "picture",
              component: () => import("@/views/archive/Picture.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/archivePlat",
              name: "archivePlat",
              component: () => import("@/views/archive/Plat.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/archivePlan",
              name: "archivePlan",
              component: () => import("@/views/archive/Plan.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/archiveTrain",
              name: "archiveTrain",
              component: () => import("@/views/archive/Train.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/archiveRule",
              name: "archiveRule",
              component: () => import("@/views/archive/Rule.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/archiveStation",
              name: "archiveStation",
              component: () => import("@/views/archive/Station.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/archiveUsers",
              name: "archiveUsers",
              component: () => import("@/views/archive/Users.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
          ],
        },
        {
          path: "/ultrasonic",
          name: "ultrasonic",
          component: () => import("@/views/Ultrasonic.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/switchMonitor",
          name: "switchMonitor",
          component: () => import("@/views/switchMonitor.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
        },
        {
          path: "/ApprovalCenter",
          name: "ApprovalCenter",
          component: () => import("@/views/ApprovalCenter/index.vue"),
          meta: {
            requireAuth: true,
            keepAlive: false,
          },
          children: [
            {
              path: "/ApprovalCenter/index",
              name: "ApprovalCenterStaging",
              component: () => import("@/views/ApprovalCenter/staging.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/ApprovalCenter/constructionUnit",
              name: "ApprovalCenterUnit",
              component: () =>
                import("@/views/ApprovalCenter/construction/unit.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/ApprovalCenter/approval",
              name: "ApprovalCenterApproval",
              component: () =>
                import("@/views/ApprovalCenter/construction/approval.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/ApprovalCenter/setting",
              name: "ApprovalCenterSetting",
              component: () => import("@/views/ApprovalCenter/setting.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
            {
              path: "/ApprovalCenter/constructionUnit/detail",
              name: "constructionUnitDetail",
              component: () =>
                import("@/views/ApprovalCenter/construction/detail.vue"),
              meta: {
                requireAuth: true,
                keepAlive: false,
              },
            },
          ],
        },
      ],
    },
    {
      path: "/IOT",
      name: "IOT",
      component: () => import("@/views/IOT.vue"),
      meta: {
        requireAuth: true,
        keepAlive: false,
      },
    },
    {
      path: "/enterprise",
      name: "enterprise",
      component: () => import("@/views/enterprise.vue"),
      meta: {
        requireAuth: true,
        keepAlive: false,
      },
    },
    {
      path: "/visualMonitoring",
      name: "visualMonitoring",
      component: () => import("@/views/visualMonitoring.vue"),
      meta: {
        requireAuth: true,
        keepAlive: false,
      },
    },
    {
      path: "/kitchenIOT",
      name: "kitchenIOT",
      component: () => import("@/views/KitchenIOT.vue"),
      meta: {
        requireAuth: true,
        keepAlive: false,
      },
    },
    {
      path: "/unitIOT",
      name: "unitIOT",
      component: () => import("@/views/UnitIOT.vue"),
      meta: {
        requireAuth: true,
        keepAlive: false,
      },
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/Login.vue"),
      meta: {
        requireAuth: false,
        keepAlive: true,
      },
    },
    {
      path: "/retrieve",
      name: "retrieve",
      component: () => import("@/views/retrieve.vue"),
      meta: {
        requireAuth: false,
        keepAlive: false,
      },
    },
  ],
});

routes.beforeEach((to, from, next) => {
  if (to.name == "home") {
    let siteInfos = utils.getStorage("SITE_INFOS");
    if (!siteInfos.token) {
      next({
        path: "/login",
      });
    }
    const { account, pwd } = to.query;
    if (!account || !pwd) {
      next();
      return;
    }
    post("/systemsetup/systemsetup/login", {
      accountname: account,
      accountpassword: pwd,
    }).then(
      (res) => {
        store.commit("setSiteInfos", res.data);
        next({
          path: "/IOT",
        });
      },
      (err) => {
        // this.$message.error(err.msg)
        // this.$router.replace("/login")
        next({
          path: "/login?loginerror=" + err.msg,
        });
      }
    );
    return;
  }
  if (to.meta.requireAuth) {
    // 判断该路由是否需要登录权限
    if (!!store.state.siteInfos) {
      // 通过vuex state获取当前的loginStatus
      next();
    } else {
      next({
        path: "/login",
      });
    }
  } else {
    next();
  }
  // next()
});
routes.afterEach((to) => {
  store.commit("setActiveMenuName", to.name);
});

export default routes;
